package com.shouhuobao.bhi.receiver;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.model.ReceiverBean;

import droid.frame.activity.base.BaseAdapterExt;

/**
 * 历史收件人 <br>
 * 现在只支持单选
 * 
 * @author coffee<br>
 *         2015-8-8上午10:11:37
 */
public class ReceiverHistoryAdapter extends BaseAdapterExt<ReceiverBean> {

	private OnClickListener clickListener;
	private ArrayList<Integer> sels = new ArrayList<Integer>();

	private int position = -1;

	public ReceiverHistoryAdapter(Activity context, ArrayList<ReceiverBean> items, OnClickListener clickListener) {
		super(items, context);
		this.clickListener = clickListener;
		setEmptyView(R.layout.receiver_history_empty);
	}

	public void setCheck(int position) {
		if (this.position == position) {
			this.position = -1;
		} else {
			this.position = position;
		}
		notifyDataSetChanged();
	}

	/**
	 * 为了支持多选, 代码先这么改
	 * 
	 * @return
	 */
	public ArrayList<Integer> getSelectItems() {
		sels.clear();
		if (position != -1) {
			sels.add(position);
		}
		return sels;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (hasEmptyView()) {
			return super.getView(position, convertView, parent);
		}
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.receiver_history_item, parent, false);
			viewHolder.mNameTv = (TextView) convertView.findViewById(R.id.receiver_name);
			viewHolder.mMobileTv = (TextView) convertView.findViewById(R.id.receiver_mobile);
			viewHolder.mAddressTv = (TextView) convertView.findViewById(R.id.receiver_address);
			viewHolder.edit = convertView.findViewById(R.id.receiver_edit);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		//
		ReceiverBean item = getItem(position);
		viewHolder.mNameTv.setText(item.getName());
		viewHolder.mMobileTv.setText(item.getPhone());
		viewHolder.mAddressTv.setText(item.getProvince() + item.getCity() + item.getDistrict() + item.getAddress());
		viewHolder.edit.setTag(item);
		viewHolder.edit.setOnClickListener(clickListener);
		return convertView;
	}

	private class ViewHolder {
		private TextView mNameTv;
		private TextView mMobileTv;
		private TextView mAddressTv;
		private View edit;//编辑
	}

}
